﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace BaseData.Common
{
    /// <summary>
    /// DataTable帮助类
    /// </summary>
    public class TableHelper
    {
        /// <summary>
        /// 从DataTable中获取满足条件的行,返回指定列columnName的值(Int)
        /// </summary>
        public static int GetIntValueFromTable(DataTable dt, string condition, string columnName)
        {
            int value = 0;
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow[] drs = dt.Select(condition);
                if (drs.Length > 0)
                {
                    if (drs[0][columnName] != DBNull.Value)
                    {
                        int.TryParse(drs[0][columnName].ToString(), out value);
                    }
                }
            }
            return value;
        }

        /// <summary>
        /// 从DataTable中获取满足条件的行,返回指定列columnName的值(String)
        /// </summary>
        public static string GetStringValueFromTable(DataTable dt, string condition, string columnName)
        {
            string value = string.Empty;
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow[] drs = dt.Select(condition);
                if (drs.Length > 0)
                {
                    if (drs[0][columnName] != DBNull.Value)
                    {
                        value = drs[0][columnName].ToString();
                    }
                }
            }
            return value;
        }

        /// <summary>
        /// 从DataRow中获取指定列columnName的值(Int)
        /// </summary>
        public static int GetIntValueFromDr(DataRow dr, string columnName)
        {
            int value = 0;
            if (dr[columnName] != null && dr[columnName] != DBNull.Value)
            {
                int.TryParse(dr[columnName].ToString(), out value);
            }
            return value;
        }

        /// <summary>
        /// 从DataRow中获取指定列columnName的值(Long)
        /// </summary>
        public static long GetLongValueFromDr(DataRow dr, string columnName)
        {
            long value = 0;
            if (dr[columnName] != null && dr[columnName] != DBNull.Value)
            {
                long.TryParse(dr[columnName].ToString(), out value);
            }
            return value;
        }

        /// <summary>
        /// 从DataRow中获取指定列columnName的值(String)
        /// </summary>
        public static string GetStringValueFromDr(DataRow dr, string columnName)
        {
            string value = string.Empty;
            if (dr[columnName] != null && dr[columnName] != DBNull.Value)
            {
                value = dr[columnName].ToString();
            }
            return value;
        }

        /// <summary>
        /// 从DataRow中获取指定列columnName的值(DateTime)
        /// </summary>
        public static DateTime GetDateTimeValueFromDr(DataRow dr, string columnName)
        {
            DateTime value = default(DateTime);
            if (dr[columnName] != null && dr[columnName] != DBNull.Value)
            {
                DateTime.TryParse(dr[columnName].ToString(), out value);
            }
            return value;
        }
    }
}